package com.agnes.network.interceptor;

import android.support.annotation.NonNull;
import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;

import java.io.IOException;

/**
 * Desc:  Post 请求拦截器
 * Creator ling
 * Date:   2017/12/18 0018 13:38
 */

public class RequestInterceptor implements Interceptor {

    private String version;

    public RequestInterceptor(String version) {
        this.version = version;
    }

    @Override
    public Response intercept(@NonNull Chain chain) throws IOException {
        Request originRequest = chain.request();
        if ("POST".equalsIgnoreCase(originRequest.method())) {
            Request.Builder builder = originRequest.newBuilder();
            builder.header("Content-Type", "application/json;charset=UTF-8")
                .header("version", version)
                // 1:iOS; 2:Android
                .header("platform", "Android")
                .method(originRequest.method(), originRequest.body());
            Request request = builder.build();
            return chain.proceed(request);
        } else {
            return chain.proceed(originRequest);
        }
    }
}
